package team.hyznrj.studentsys.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import team.hyznrj.studentsys.dao.mapper.ConductScoreMapper;
import team.hyznrj.studentsys.dao.repository.AcademicYearsRepository;
import team.hyznrj.studentsys.dao.repository.SkillMarkRepository;
import team.hyznrj.studentsys.entity.AcademicYears;
import team.hyznrj.studentsys.entity.SkillMark;
import team.hyznrj.studentsys.entity.table.ScoreSkillTable;
import team.hyznrj.studentsys.enums.MarkEnum;
import team.hyznrj.studentsys.exception.MarkException;
import team.hyznrj.studentsys.service.SkillMarkService;
import team.hyznrj.studentsys.utils.KeyUtil;

import java.util.List;

/**
 * @author ReMidDream
 * @date 2018-04-11 13:11
 **/
@Service
@Slf4j
public class SkillMarkServiceImpl implements SkillMarkService {

    @Autowired
    private AcademicYearsRepository academicYearsRepository;
    @Autowired
    private SkillMarkRepository skillMarkRepository;
    @Autowired
    private ConductScoreMapper conductScoreMapper;

    @Override
    public List<SkillMark> findAllSkillMark() {
        return skillMarkRepository.findAll();
    }

    @Override
    public List<SkillMark> findSkillMarkByYearOrTerm(Integer year, Integer term) {
        if (StringUtils.isEmpty(year) && StringUtils.isEmpty(term)) {
            log.error(" @根据学年/学期查询技术技能 : 参数错误,year={},term={}",year,term);
            throw new MarkException(MarkEnum.PARAMRMETER_ERROR);
        }
        List<SkillMark> skillMarks = null;
        if (!StringUtils.isEmpty(year) && StringUtils.isEmpty(term)) {
            skillMarks = skillMarkRepository.findByAcademicYear(year);
        }
        if (StringUtils.isEmpty(year) && !StringUtils.isEmpty(term)) {
            skillMarks = skillMarkRepository.findBySchoolTerm(term);
        }
        if (!StringUtils.isEmpty(year) && !StringUtils.isEmpty(term)) {
            skillMarks = skillMarkRepository.findByAcademicYearAndSchoolTerm(year,term);
        }
        return skillMarks;
    }

    @Override
    public boolean insertOrUpdateSkillMark(SkillMark skillMark) {
        //判断是更新还是插入
        if(skillMark.getId() != null){
            //更新操作
            SkillMark check = skillMarkRepository.findOne(skillMark.getId());
            //判断是非存在此实体
            if (check == null) {
                log.error(" @更新技术技能分:不存在实体 id={}",skillMark.getId());
                throw  new MarkException(MarkEnum.PARAMRMETER_ERROR);
            }
            SkillMark save = skillMarkRepository.save(skillMark);
            if (save != null) {
                throw new MarkException(MarkEnum.UPDATE_ERROR);
            }
            return  true;
        }
        //插入操作
        skillMark.setId(KeyUtil.getUniqueKey());
        SkillMark save = skillMarkRepository.save(skillMark);
        if (save != null) {
            throw new MarkException(MarkEnum.UPDATE_ERROR);
        }
        return  true;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean delectSkillMark(List<String> ids) {
        List<ScoreSkillTable> tableList = conductScoreMapper.findScoreSkillTableBySkillMarkIds(ids);
        if (!CollectionUtils.isEmpty(tableList)) {
            log.error(" @删除技术技能分: 该数据已被学生使用,tableList={}",tableList);
            throw  new MarkException(MarkEnum.DATE_USED);
        }
        for (String id : ids) {
            skillMarkRepository.delete(id);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean forcedDelectSkillMark(List<String> ids) {
        int result = conductScoreMapper.deleteScoreSkillTableBySkillMarkIds(ids);
        if (result < 0) {
            log.error( "@强制删除技术技能分: 数据库异常");
            throw  new MarkException(MarkEnum.DELETE_ERROR);
        }
        for (String id : ids) {
            skillMarkRepository.delete(id);
        }
        return true;
    }


}
